const app = getApp();
var amapFile = require('../../lib/amap-wx.130.js');//如：..­/..­/libs/amap-wx.js
export { }
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isFristPage: {
      type: Boolean,
      value: false
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    weather:{},
    bgimg:"url('http://localhost:9090/images/晴.png')",
    titleHeight: {},
    capsuleObj: {},
    weatherIcon:'http://localhost:9090/images/晴_1.png',
    location: '',//接收用户的位置信息【格式：{经度，纬度}】
   iconWeatherMap :{
      "风": ['有风', '平静', '微风', '和风', '清风', '强风/劲风', '疾风', '大风', '烈风', '风暴', '狂爆风', '飓风', '热带风暴', '龙卷风'],
      "多云": ['少云', '晴间多云', '多云'],
      "雪": ['雪', '阵雪', '小雪', '中雪', '大雪', '暴雪', '小雪-中雪', '中雪-大雪', '大雪-暴雪', '冷'],
      "雾": ['浮尘', '扬沙', '沙尘暴', '强沙尘暴', '雾', '浓雾', '强浓雾', '轻雾', '大雾', '特强浓雾'],
      "晴": ['晴', '热'],
      "雨夹雪": ['雨雪天气', '雨夹雪', '阵雨夹雪'],
      "雨": ['阵雨', '雷阵雨', '雷阵雨并伴有冰雹', '小雨', '中雨', '大雨', '暴雨', '大暴雨', '特大暴雨', "强阵雨", '强雷阵雨', '极端降雨', '毛毛雨/细雨', '雨', '小雨-中雨', '中雨-大雨', '大雨-暴雨', '暴雨-大暴雨', '大暴雨-特大暴雨', '冻雨'],
      "阴": ['阴', '霾', '中度霾', '重度霾', '严重霾', '未知']
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getIcon(weather:String){
      let that=this;
      let url = "url('http://localhost:9090/images/晴天.png')";
      let icon = "http://localhost:9090/images/晴天_1.png";
      for (const weatherKey in this.data.iconWeatherMap) {
        if (Object.hasOwnProperty.call(this.data.iconWeatherMap, weatherKey)) {
          const weatherNames = (this.data.iconWeatherMap as any)[weatherKey];
          const findWeatherItem = weatherNames.find((name: String) => weather === name)
          // 如果找了某一类的图标了，那重新赋值url
          if (findWeatherItem) {
            // 这里的weatherKey和icon的名字一一对应了
            url = `url('http://localhost:9090/images/${weatherKey}.png')`;
            icon = `http://localhost:9090/images/${weatherKey}_1.png`;
            // console.debug('@find weather key = ', weatherKey)
            that.setData({
              bgimg:url,
              weatherIcon:icon
            });
            break;
          }
        }
      }
    },
    getWeather(){
      let thah=this;
      var myAmapFun = new amapFile.AMapWX({key:'0c22407f484d121a136efe773ae2075e'});
      myAmapFun.getRegeo({
        location: this.data.location,
        success: function(data:any){
          //成功回调
          console.log(data);
          myAmapFun.getWeather({
           // city:'株洲',
            city:data[0].regeocodeData.addressComponent.city,
            success: function(resdata:any){
              //成功回调
              thah.setData({
                weather:resdata
              })
              thah.getIcon(resdata.weather.data);
            
            },
            fail: function(info:any){
              //失败回调
              console.log(info)
            }
          })
        },
        fail: function(info:any){
          //失败回调
          console.log(info)
        }
      })
    },
    //获取当前位置信息。
    getLocation() {
      var that = this
      wx.getLocation({
        type: 'gcj02',
        success(res) {
          that.setData({
            location: res.longitude + "," + res.latitude
          })
           console.log("地理坐标",res)
           that.getWeather()
          // that.getCityByLoaction()
        }, fail(err:any) {
          console.log(err);
          wx.showModal({
            title: '获取定位信息失败',
            content: '为了给您提供准确的天气预报服务,请在设置中授权【位置信息】',
            success(mRes) {
              if (mRes.confirm) {
                wx.openSetting({
                  success: function (data) {
                    if (data.authSetting["scope.userLocation"] === true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      that.getLocation()
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                      that.setData({
                        location: "113.18,27.96"
                      })
                       that.getWeather()
                      // that.getCityByLoaction()
                    }
                  }, fail(err) {
                    console.log(err)
                    wx.showToast({
                      title: '唤起设置页失败，请手动打开',
                      icon: 'none',
                      duration: 1000
                    })
                    that.setData({
                      location: "113.18,27.96"
                    })
                    that.getWeather()
                    // that.getCityByLoaction()
                  }
                })
              } else if (mRes.cancel) {
                that.setData({
                  location: "113.18,27.96"
                })
                 that.getWeather()
                // that.getCityByLoaction()
              }
            }
          })
        }
      })
    },
  },
  attached() {
  
    this.setData({
      titleHeight: app.globalData.titleHeight,
      capsuleObj: app.globalData.capsuleObj
    });
    //如果是首页，获取位置信息
    if (this.properties.isFristPage) {
      this.getLocation();
    }
  },
})